package com.jxl_dyw.UI;

import com.jxl_dyw.entity.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class EditEmpUI {
    public EditEmpUI(Employee employee) {
        // 创建 JFrame
        JFrame frame = new JFrame("编辑员工");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 关闭时只关闭当前窗口，不退出整个程序
        frame.setSize(800, 600); // 增大窗口尺寸
        frame.setLocationRelativeTo(null); // 居中显示窗口

        // 使用 GridBagLayout 进行布局
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 增大组件之间的间距

        // 创建一个面板用于存放所有输入字段
        JPanel inputPanel = new JPanel(new GridBagLayout());
        gbc.gridx = 0;
        gbc.gridy = 0;

        // 设置全局字体，使所有组件的文字更大
        Font font = new Font("宋体", Font.PLAIN, 18);

        // 创建组件并添加到面板
        JLabel empIdLabel = new JLabel("员工ID:");
        JTextField empIdField = new JTextField(20);
        empIdLabel.setFont(font);
        empIdField.setFont(font);
        addComponent(inputPanel, empIdLabel, empIdField, gbc);

        JLabel nameLabel = new JLabel("姓名:");
        JTextField nameField = new JTextField(20);
        nameLabel.setFont(font);
        nameField.setFont(font);
        addComponent(inputPanel, nameLabel, nameField, gbc);

        JLabel sexLabel = new JLabel("性别:");
        JTextField sexField = new JTextField(20);
        sexLabel.setFont(font);
        sexField.setFont(font);
        addComponent(inputPanel, sexLabel, sexField, gbc);

        JLabel birthdayLabel = new JLabel("出生年月:");
        JTextField birthdayField = new JTextField(20);
        birthdayLabel.setFont(font);
        birthdayField.setFont(font);
        addComponent(inputPanel, birthdayLabel, birthdayField, gbc);

        JLabel workdayLabel = new JLabel("工作年月:");
        JTextField workdayField = new JTextField(20);
        workdayLabel.setFont(font);
        workdayField.setFont(font);
        addComponent(inputPanel, workdayLabel, workdayField, gbc);

        JLabel educationLabel = new JLabel("学历:");
        JTextField educationField = new JTextField(20);
        educationLabel.setFont(font);
        educationField.setFont(font);
        addComponent(inputPanel, educationLabel, educationField, gbc);

        // 部门标签
        JLabel positionLabel = new JLabel("部门:");
        positionLabel.setFont(font);
        //获取部门哈希表中所有的键
        String[] departments = DeptUI.getDeptMap().keys().toArray(new String[0]);
        // 创建下拉框，并添加预定义的部门选项
        JComboBox<String> positionComboBox = new JComboBox<>(departments);
        positionComboBox.setFont(font);
        // 将标签和下拉框添加到面板中
        addComponent(inputPanel, positionLabel, positionComboBox, gbc);

        JLabel addressLabel = new JLabel("住址:");
        JTextField addressField = new JTextField(20);
        addressLabel.setFont(font);
        addressField.setFont(font);
        addComponent(inputPanel, addressLabel, addressField, gbc);

        JLabel phoneLabel = new JLabel("电话:");
        JTextField phoneField = new JTextField(20);
        phoneLabel.setFont(font);
        phoneField.setFont(font);
        addComponent(inputPanel, phoneLabel, phoneField, gbc);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("修改");
        addButton.setFont(font); // 设置按钮字体
        buttonPanel.add(addButton);

        //将获取的对象的数据放入各个输入框内
        empIdField.setText(employee.getEmpId());
        nameField.setText(employee.getName());
        sexField.setText(employee.getSex());
        birthdayField.setText(employee.getBirthday());
        workdayField.setText(employee.getWorkday());
        educationField.setText(employee.getEducation());
        positionComboBox.setSelectedItem(employee.getPosition());
        addressField.setText(employee.getAddress());
        phoneField.setText(employee.getPhone());

        //用户姓名不允许修改
        nameField.setEditable(false);

        // 添加按钮点击事件监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的员工信息
                String empId = empIdField.getText().trim();
                String name = nameField.getText().trim();
                String sex = sexField.getText().trim();
                String birthday = birthdayField.getText().trim();
                String workday = workdayField.getText().trim();
                String education = educationField.getText().trim();
                String position = positionComboBox.getSelectedItem().toString();
                String address = addressField.getText().trim();
                String phone = phoneField.getText().trim();
                //验证输入出生日期格式
                if (!birthday.matches("\\d{4}-\\d{2}-\\d{2}")) {
                    JOptionPane.showMessageDialog(frame, "出生年月格式不正确，请输入yyyy-MM-dd格式！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //验证输入工作日期格式
                if (!workday.matches("\\d{4}-\\d{2}-\\d{2}")) {
                    JOptionPane.showMessageDialog(frame, "工作年月格式不正确，请输入yyyy-MM-dd格式！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                // 验证输入是否为空
                if (empId.isEmpty() || name.isEmpty() || sex.isEmpty() || birthday.isEmpty() || workday.isEmpty() ||
                        education.isEmpty() || position.isEmpty() || address.isEmpty() || phone.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "请填写所有必填项！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 创建 Employee 对象
                Employee employee = new Employee(empId, name, sex, birthday, workday, education, position, address, phone);

                // 将 Employee 对象添加到哈希表中，以员工的名字作为键
                EmpUI.getEmployeeMap().put(employee.getName(), employee);
                //刷新表格
                EmpUI.refreshTable();
                //将员工信息写入文件
                try {
                    EmpUI.writeEmployeesToFile(EmpUI.filePath);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }

                // 清空输入框
                empIdField.setText("");
                nameField.setText("");
                sexField.setText("");
                birthdayField.setText("");
                workdayField.setText("");
                educationField.setText("");
                positionComboBox.setSelectedItem("");
                addressField.setText("");
                phoneField.setText("");
                //关闭页面
                frame.dispose();
            }
        });

        // 将输入面板和按钮面板添加到主窗口
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2; // 占两列
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        frame.add(inputPanel, gbc);

        gbc.gridy++;
        gbc.gridwidth = 1; // 占一列
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.weighty = 0.1;
        frame.add(buttonPanel, gbc);

        // 设置窗口可见
        frame.setVisible(true);
    }

    // 辅助方法：添加标签和输入框到面板
    private void addComponent(JPanel panel, JLabel label, JComponent textField, GridBagConstraints gbc) {
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(label, gbc);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(textField, gbc);
    }
}
